<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;


class UserController extends Controller
{
    // 个人设置页面
    public function setting(){
        $user = \Auth::user();
        return view('user.setting',compact('user'));
    }
    // 个人设置操作
    public function settingStore(Request $request){
        // 验证
        $this->validate(request(),[
           'name'=> 'required|min:2|max:15',
        ]);
        // 逻辑
        $name = request('name');
        $user =\Auth::user();
        if($name !=$user->name){
            if(User::where('name',$name)->count()>0){
                return back()->withErrors('用户名称已经被注册!');
            }
            $user->name = $name;
        }
        if($request->file('avatar')){
            $path = $request->file('avatar')->storePublicly($user->id);
            $user->avatar ='storage/'.$path;
        }
        $user->save();
        // 渲染
        return back();

    }
    // 个人中心页面
    public function show(User $user){
        // 这个人的信息,包含关注.粉丝.文章数
        $user = User::withCount(['posts','stars','fans'])->find($user->id);
        // 这个人的文章列表,取创建时间最新10条
        $posts = $user->posts()->orderBy('created_at','desc')->take(10)->get();
        // 这个人关注的用户,包含关注用户的粉丝,关注,文章数
        $stars = $user->stars;
        $susers = User::whereIn('id',$stars->pluck('star_id'))->withCount(['posts','stars','fans'])->get();
        // 这个人的粉丝用户,包含粉丝用户的粉丝,关注,文章数
        $fans = $user->fans;
        $fusers = User::whereIn('id',$fans->pluck('fan_id'))->withCount(['posts','stars','fans'])->get();
        return view('user.show',compact('user','posts','susers','fusers'));
    }
    // 关注用户
    public function fan(User $user){
        $me = \Auth::user();
        $me->dofan($user->id);
        return [
            'status'=>0,
            'msg'=>'关注成功',
        ];
    }
    // 取消关注
    public function unfan(User $user){
        $me = \Auth::user();
        $me->doUnfan($user->id);
        return [
            'status'=>0,
            'msg'=>'取消关注成功',
        ];
    }
}
